<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="translate(title)"
    width="870"
    @close="close"
  >
    <el-form
      ref="formRef"
      inline
      label-width="150px"
      :model="form"
      :rules="rules"
    >
      <el-form-item :label="translate('仓库主键') + '：'" prop="whid">
        <el-select v-model="form.whid" :placeholder="translate('请选择')">
          <el-option
            v-for="item in tree"
            :key="item.WHID"
            :label="item.FullName"
            :value="item.WHID"
          />
        </el-select>
        <!-- <el-tree-select
          v-model="form.whid"
          :check-strictly="true"
          :data="tree"
          node-key="WHID"
          :props="defaultProps"
          :render-after-expand="false"
          show-checkbox
        /> -->
      </el-form-item>
      <el-form-item :label="translate('库区编码') + '：'" prop="code">
        <span v-if="code">{{ code }}-</span>
        <el-input
          v-model="form.code"
          :input-style="{ width: code ? '172px' : '212px' }"
        />
      </el-form-item>
      <el-form-item :label="translate('库区名称') + '：'" prop="fullName">
        <el-input v-model="form.fullName" />
      </el-form-item>
      <el-form-item :label="translate('仓库类型') + '：'" prop="whaType">
        <!-- <el-input v-model="form.WHAType" /> -->
        <el-select v-model="form.whaType" :placeholder="translate('请选择')">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="translate('库区面积') + '：'" prop="area">
        <el-input v-model="form.area" />
      </el-form-item>
      <el-form-item :label="translate('显示顺序') + '：'" prop="sortCode">
        <el-input v-model="form.sortCode" />
      </el-form-item>
      <el-form-item :label="translate('是否启用') + '：'" prop="enabled">
        <el-switch
          v-model="form.enabled"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item :label="translate('描述') + '：'">
        <el-input
          v-model="form.remark"
          autosize
          :placeholder="translate('请输入描述')"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">{{ translate('取消') }}</el-button>
      <el-button type="primary" @click="save">
        {{ translate('确定') }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  // import VabIconSelector from '@/plugins/VabIconSelector'
  import { doEdit, getwhainfo } from '@/api/area'
  import { watch, ref } from 'vue'
  import { translate } from '@/i18n'
  export default defineComponent({
    name: 'StoreEdit',
    props: {
      tree: {
        type: Array,
        default: () => [],
      },
    },
    // components: { VabIconSelector },
    emits: ['fetchData'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        params: {},
        queryForm: {
          whid: 'string',
        },
        parentList: [],
        list: [],
        areaList: [],
        storeList: [],
        options: [
          {
            value: '备货区',
            label: '备货区',
          },
          {
            value: '拣货区',
            label: '拣货区',
          },
          {
            value: '暂存区',
            label: '暂存区',
          },
          {
            value: '活动区',
            label: '活动区',
          },
          {
            value: '次品区',
            label: '次品区',
          },
        ],
        defaultProps: {
          children: 'ChildNodes',
          label: 'FullName',
        },
        form: {
          meta: {},
        },
        rules: {
          whid: [
            {
              required: true,
              trigger: 'change',
              message: translate('请选择库区主键'),
            },
          ],
          code: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入库区编码'),
            },
          ],
          fullName: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入库区名称'),
            },
          ],
          area: [
            {
              required: false,
              trigger: 'blur',
              message: translate('请输入库区面积'),
            },
          ],
          enabled: [
            {
              required: false,
              trigger: 'blur',
              message: translate('请选择是否可用'),
            },
          ],
          sortCode: [
            {
              required: false,
              trigger: 'change',
              message: translate('请输入显示顺序'),
            },
          ],
          remark: [
            {
              required: false,
              trigger: 'change',
              message: translate('请输入描述'),
            },
          ],
          whaType: [
            {
              required: true,
              trigger: 'blur,change',
              message: translate('请选择库区类型'),
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      })
      const code = ref('')
      watch(
        () => state.form.whid,
        () => {
          code.value = props.tree.filter((item) => {
            if (item.WHID === state.form.whid) {
              return item
            }
          })[0]?.Code
        }
      )
      //获取详情数据
      const getWhainfo = async () => {
        state.listLoading = true
        const {
          data: { data },
        } = await getwhainfo(state.queryForm)
        data.WHAID = data.whaid
        state.form = data
        state.listLoading = false
      }
      //编辑传值
      const showEdit = (row, type) => {
        if (type != 'edit') {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.queryForm = { whaid: row.WHAID }
          getWhainfo()
        }
        state.dialogFormVisible = true
      }
      //弹框关闭
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          meta: {
            icon: '',
          },
        }
        state.dialogFormVisible = false
      }
      //数据保存
      const save = async () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { message } = await doEdit(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            close()
            emit('fetchData')
          }
        })
      }
      return {
        ...toRefs(state),
        showEdit,
        getWhainfo,
        close,
        save,
        code,
        translate,
      }
    },
  })
</script>
<style scoped>
  .el-form {
    display: flex;
    flex-flow: wrap;
    justify-content: left;
  }
  ::v-deep .el-input {
    width: 234px;
  }
  .el-form-item {
    width: 380px;
  }
</style>
